package com.helen.ui.account;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.coorchice.library.SuperTextView;
import com.helen.adapter.RechargeListAdapter;
import com.helen.db.UserHelper;
import com.helen.entity.RechargeEntity;
import com.helen.global.Constants;
import com.helen.shopping.BuildConfig;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.RxPayUtils;
import com.helen.utils.tiputils.MToast;
import com.helen.widget.MyEditText;
import com.weilu.pay.annotation.WXPay;
import com.weilu.pay.api.RxWxPay;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/5/22.
 * 充值
 */
@WXPay(BuildConfig.APPLICATION_ID)
public class RechargeActivity extends BaseActivity {
    @BindView(R.id.tv_current_balance)
    TextView tvCurrentBalance;
    @BindView(R.id.et_amount)
    MyEditText etAmount;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.cb_zhifubao)
    CheckBox cbZhifubao;
    @BindView(R.id.cb_weixin)
    CheckBox cbWeixin;
    @BindView(R.id.tv_recharge)
    SuperTextView tvRecharge;
    private List<RechargeEntity> list = new ArrayList<>();
    private RechargeListAdapter rechargeListAdapter;
    private String amount;
    private int rechargeId = 0;
    private String pay_method = "1";


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.recharge);

        initData();
        etAmount.addTextChangedListener(new EditChangedListener());
        etAmount.setSelection(etAmount.getText().toString().length());
        rechargeListAdapter = new RechargeListAdapter(R.layout.item_recharge_list, list);
        recyclerView.setLayoutManager(new GridLayoutManager(RechargeActivity.this, 3));
        recyclerView.setAdapter(rechargeListAdapter);
        rechargeListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RechargeEntity rechargeEntity = (RechargeEntity) adapter.getData().get(position);
                if (rechargeListAdapter.getSelectedPostion() == position) {
                    rechargeListAdapter.setSelectedPostion(-1);
                    rechargeId = 0;
                } else {
                    etAmount.setText("");
                    rechargeId = rechargeEntity.getId();
                    rechargeListAdapter.setSelectedPostion(position);
                }
                rechargeListAdapter.notifyDataSetChanged();
            }
        });

        RefreshMoneyUtils refreshMoneyUtils = new RefreshMoneyUtils();
        refreshMoneyUtils.refreshMoneyApi(RechargeActivity.this);
        refreshMoneyUtils.setOnResultListener(new RefreshMoneyUtils.OnResultListener() {
            @Override
            public void onResult(double money, int jinbao) {
                tvCurrentBalance.setText(money + "");
            }
        });

    }

    @Override
    protected void updateViews() {

    }


    private void initData() {
        list.add(new RechargeEntity("10", 1));
        list.add(new RechargeEntity("20", 2));
        list.add(new RechargeEntity("50", 3));
        list.add(new RechargeEntity("100", 4));
        list.add(new RechargeEntity("200", 5));
        list.add(new RechargeEntity("500", 6));
    }

    class EditChangedListener implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            amount = etAmount.getText().toString().trim();
            if (amount.length() > 0) {
                rechargeListAdapter.setSelectedPostion(-1);
                rechargeListAdapter.notifyDataSetChanged();
                rechargeId = 0;
            }


        }
    }

    @OnClick({R.id.cb_zhifubao, R.id.cb_weixin, R.id.tv_recharge})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cb_zhifubao:
                cbZhifubao.setChecked(true);
                cbWeixin.setChecked(false);
                pay_method = "1";
                break;
            case R.id.cb_weixin:
                pay_method = "2";
                cbWeixin.setChecked(true);
                cbZhifubao.setChecked(false);
                break;
            case R.id.tv_recharge:
                if (TextUtils.isEmpty(amount) && rechargeId == 0) {
                    MToast.makeTextLong(RechargeActivity.this, "请选择或者输入充值金额").show();
                    return;
                }
                rechargeApi();
                break;
        }
    }

    //    充值接口
    private void rechargeApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("userid", UserHelper.getInstance().getloginEntity(RechargeActivity.this).getId() + "");
        if (rechargeId != 0) {
            params.put("money", list.get(rechargeId - 1).getPrice());
        } else {
            params.put("money", amount);
        }
        params.put("pay_method", pay_method); //1、支付宝 2、微信
        MyLog.e("yang", "params==" + params);
        EasyHttp.post(Constants.API_JSONAPI_RECHARGE)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "充值失败==" + e.toString());
                        MToast.makeTextShort(RechargeActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "充值==" + result);
                        int code = JsonUtil.getJsonValuesInt(result, "code");
                        String msg = JsonUtil.getJsonValuesString(result, "msg");
                        if (code == 0) {
                            String mResult = JsonUtil.getJsonValuesString(result, "result");
                            if (pay_method.equals("1")) {
                                RxPayUtils.aLiPay(RechargeActivity.this, mResult);
                            } else {
                                RxPayUtils.wechatPay(RechargeActivity.this,mResult);
                            }

                        } else {
                            MToast.makeTextShort(RechargeActivity.this, msg).show();
                        }

                    }
                });
    }





    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxWxPay.getInstance().onDestroy();
    }
}
